package com.lei.eduservice.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lei.eduservice.entity.EduCourse;
import com.lei.eduservice.entity.EduCourseDescription;
import com.lei.eduservice.entity.vo.CourseInfoVo;
import com.lei.eduservice.entity.vo.CoursePublishVo;
import com.lei.eduservice.entity.vo.frontvo.CourseWebVo;
import com.lei.eduservice.entity.vo.frontvo.FCouseQueryVo;
import com.lei.eduservice.mapper.EduCourseMapper;
import com.lei.eduservice.service.EduChapterService;
import com.lei.eduservice.service.EduCourseDescriptionService;
import com.lei.eduservice.service.EduCourseService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lei.eduservice.service.EduVideoService;
import com.lei.servicebase.exception.GuliException;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.HashMap;

/**
 * <p>
 * 课程 服务实现类
 * </p>
 *
 * @author 熊磊
 * @since 2020-06-04
 */
@Service
public class EduCourseServiceImpl extends ServiceImpl<EduCourseMapper, EduCourse> implements EduCourseService {
    @Autowired
    private EduCourseDescriptionService descriptionService;
    @Autowired
    private EduVideoService videoService;
    @Autowired
    private EduChapterService chapterService;
    @Override
    public String saveCourseInfo(CourseInfoVo courseInfoVo) {
        //向课程表添加信息
        EduCourse eduCourse = new EduCourse();
        BeanUtils.copyProperties(courseInfoVo,eduCourse);
        if (baseMapper.insert(eduCourse)==0){
            throw new GuliException(20001,"添加课程信息失败");
        }


        //向课程简介表添加简介信息
        EduCourseDescription eduCourseDescription = new EduCourseDescription();
        //手动设置简介ID,与课程ID一致
        String id = eduCourse.getId();
        eduCourseDescription.setId(id);
        eduCourseDescription.setDescription(courseInfoVo.getDescription());
        descriptionService.save(eduCourseDescription);

        return id;
    }

    //根据Id获取CourseInfoVo做数据回显
    @Override
    public CourseInfoVo getCourseInfo(String courseId) {
        CourseInfoVo courseInfoVo = new CourseInfoVo();
        //查询课程表
        EduCourse eduCourse = baseMapper.selectById(courseId);
        BeanUtils.copyProperties(eduCourse,courseInfoVo);
        //查询描述表
        EduCourseDescription courseDescription = descriptionService.getById(courseId);
        courseInfoVo.setDescription(courseDescription.getDescription());
        return courseInfoVo;
    }

    @Override
    public void updateCourseInfo(CourseInfoVo courseInfoVo) {
        //修改课程表
        EduCourse eduCourse = new EduCourse();
        BeanUtils.copyProperties(courseInfoVo,eduCourse);
        if (baseMapper.updateById(eduCourse)==0){
            throw new GuliException(20001,"修改课程信息失败");
        }
        //修改描述表
        EduCourseDescription description = new EduCourseDescription();
        description.setId(courseInfoVo.getId());
        description.setDescription(courseInfoVo.getDescription());
        descriptionService.updateById(description);
    }

    @Override
    public CoursePublishVo publishCourseInfo(String courseId) {
        return baseMapper.getPublishCourseInfo(courseId);
    }

    @Override
    public void deleteCourse(String courseId) {
        // 根据课程Id删除小节,同时删除视频
        videoService.removeByCourseId(courseId);
        // 根据课程Id删除章节
        chapterService.removeByCourseId(courseId);
        // 根据课程Id删除描述
        descriptionService.removeById(courseId);
        // 根据课程Id删除课程
        int i = baseMapper.deleteById(courseId);
        if (i==0) {
            throw new GuliException(20001,"删除失败");
        }
    }

    //前台的分页查询
    @Override
    public HashMap<String, Object> getFrontCourseList(Page<EduCourse> coursePage, FCouseQueryVo courseVo) {
        QueryWrapper<EduCourse> wrapper = new QueryWrapper<>();
        wrapper.eq("status","Normal");
        if (!StringUtils.isEmpty(courseVo.getSubjectParentId())){  //一级分类
            wrapper.eq("subject_parent_id",courseVo.getSubjectParentId());
        }
        if (!StringUtils.isEmpty(courseVo.getSubjectId())){  //二级分类
            wrapper.eq("subject_id",courseVo.getSubjectId());
        }
        if (!StringUtils.isEmpty(courseVo.getBuyCountSort())){   //关注度
            wrapper.orderByDesc("buy_count");
        }
        if (!StringUtils.isEmpty(courseVo.getGmtCreateSort())){   //时间排序
            wrapper.orderByDesc("gmt_create");
        }
        if (!StringUtils.isEmpty(courseVo.getPriceSort())){  //价格排序
            wrapper.orderByDesc("price");
        }
        baseMapper.selectPage(coursePage,wrapper);

        HashMap<String, Object> map = new HashMap<>();
        map.put("records",coursePage.getRecords());
        map.put("current",coursePage.getCurrent());
        map.put("size",coursePage.getSize());
        map.put("total",coursePage.getTotal());
        map.put("pages",coursePage.getPages());
        map.put("hasNext",coursePage.hasNext());
        map.put("hasPrevious",coursePage.hasPrevious());
        return map;
    }

    //前台页面显示课程详情的方法
    @Override
    public CourseWebVo getBaseCourseInfo(String courseId) {
        return baseMapper.getBaseCourseInfo(courseId);
    }
}
